package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.util.HibernateUtil;

public class SAPERPModuleDAO {
	
	public SAPERPModule createSAPERPModule(SAPERPModule sapErpModule){
		try{
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.getTransaction().begin();
		System.out.println("FLUSH MODE: " + session.getFlushMode());
		session.setFlushMode(FlushMode.COMMIT);
		session.save(sapErpModule);
		session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
		return sapErpModule;
		
	}
	public List<SAPERPModule> getSAPERPModules()
	{
		List<SAPERPModule> sapERPModules = null;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);
			Query query = session.createQuery("from SAPERPModule s");
			query.setCacheable(false);
			sapERPModules = (List<SAPERPModule>) query.list();
			session.getTransaction().commit();
	}
		catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return sapERPModules;
	}
	
	public SAPERPModule getSAPERPModuleBySAPERPModuleId(Long sapERPModuleId) {
		 SAPERPModule technology = null;
			try {
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.COMMIT);
				Query query = session.createQuery("from SAPERPModule sm where sm.SAPERPModuleId=:sapERPModuleId").setLong(
						"sapERPModuleId", sapERPModuleId);;
				query.setCacheable(false);
				List<SAPERPModule> tempList = query.list();
				session.getTransaction().commit();
				
				if(tempList != null && tempList.size()>0){
					technology = tempList.get(0);
					return technology;
				}else{
					return null;
				}
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}		
		}
	
	
	
}
